|
|
|
|
|
|
|
The code in Listing 5.6 establishes the integer variables for the high and low in the range, sets them to 100 and 1 respectively, and then checks the value entered into the txtHighLow text control. If the value is out of the allowed range, a message is displayed, and the input cursor is moved back to the field that contains the invalid data. Notice that the message not only tells the user that the data is invalid, it also tells the user what values are acceptable. If the data entered is within range, the program exits normally. |
|
|
|
|
|
|
|
|
Now save and run the program. If you skip to the OK button without entering data or enter data outside the allowed range, you see the validation message. |
|
|
|
|
|
|
|
|
Independent Content ValidationMin/Max Field Lengths |
|
|
|
|
|
|
|
|
Another common form-level validation step is to make sure that character strings meet the minimum or maximum length requirements. This is done in the same way numeric values are checked for high and low ranges. |
|
|
|
|
|
|
|
|
Let's add input validation to ensure that the Uppercase textbox you placed on the form earlier is no longer than 10 characters, and at least 3 characters in length. You just need to add the code in Listing 5.7 to the cmdOK_click event that checks the txtUpper field for length. |
|
|
|
|
|
|
|
|
Listing 5.7. The form-level validation routine to check the length of fields and a valid range of values. |
|
|
|
|
|
|
|
|
Private Sub cmdOK_Click()
'
Dim intHigh As Integer
Dim intLow As Integer
Dim strHighLowMsg As String
'
Dim intMinLen As Integer
Dim intMaxLen As Integer
Dim strMinMaxMsg As String
'
Dim blnOK As Boolean
'
intHigh = 100
intLow = 1
strHighLowMsg = "High/Low field must contain a value between " & _
CStr(intLow) & " and " & CStr(intHigh)
'
intMinLen = 3
intMaxLen = 10
strMinMaxMsg = "Upper field must be between " & _
CStr(intMinLen) & " and " & CStr(intMaxLen) & " long."
' |
|
|
|
|
|